package Students

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/Students"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
    StudentsReq "github.com/flipped-aurora/gin-vue-admin/server/model/Students/request"
	"fmt"
)

type Student_structService struct {
}

// CreateStudent_struct 创建Student_struct记录
// Author [piexlmax](https://github.com/piexlmax)
func (student_miniService *Student_structService) CreateStudent_struct(student_mini *Students.Student_struct) (err error) {
	fmt.Println("chuan",student_mini)
	err = global.GVA_DB.Create(student_mini).Error
	return err
}

// DeleteStudent_struct 删除Student_struct记录
// Author [piexlmax](https://github.com/piexlmax)
func (student_miniService *Student_structService)DeleteStudent_struct(student_mini Students.Student_struct) (err error) {
	err = global.GVA_DB.Delete(&student_mini).Error
	return err
}

// DeleteStudent_structByIds 批量删除Student_struct记录
// Author [piexlmax](https://github.com/piexlmax)
func (student_miniService *Student_structService)DeleteStudent_structByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]Students.Student_struct{},"id in ?",ids.Ids).Error
	return err
}

// UpdateStudent_struct 更新Student_struct记录
// Author [piexlmax](https://github.com/piexlmax)
func (student_miniService *Student_structService)UpdateStudent_struct(student_mini Students.Student_struct) (err error) {
	err = global.GVA_DB.Save(&student_mini).Error
	return err
}

// GetStudent_struct 根据id获取Student_struct记录
// Author [piexlmax](https://github.com/piexlmax)
func (student_miniService *Student_structService)GetStudent_struct(id uint) (student_mini Students.Student_struct, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&student_mini).Error
	return
}

// GetStudent_structInfoList 分页获取Student_struct记录
// Author [piexlmax](https://github.com/piexlmax)
func (student_miniService *Student_structService)GetStudent_structInfoList(info StudentsReq.Student_structSearch) (list []Students.Student_struct, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&Students.Student_struct{})
    var student_minis []Students.Student_struct
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.Name != "" {
        db = db.Where("name = ?",info.Name)
    }
    if info.Age != nil {
        db = db.Where("age > ?",info.Age)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	err = db.Limit(limit).Offset(offset).Find(&student_minis).Error
	return  student_minis, total, err
}
